// ----------------------------
//     // // /    //   // // //
//    //         //   //
//   // //      //   // // //
//        //   //   //      //
//       //   //    //      //
// // //     //      // // //
// ----------------------------
// -      timeScaleView       -
// ----------------------------
(function(ב) {
    // 开启严格模式
    "use strict";

    ב.timeScale = function(type, path, time) {
    	// 初始化
    	$(".wrap_timeScale").show();
    	let listBox = $(".timeBox .timeList");
    	listBox.empty();

    	// 实例化日历
    	$("#formDate").datetimepicker('remove');

    	// 判断类型
    	if (type == 'ri') {
    		timeDay(time);
    	} else if (type == 'xun') {
    		timeTenDays(time);
    	} else if (type == 'yue') {
    		timeMonth(time);
    	} else {
    		timeYear(time);
    	}

    	// 日
    	function timeDay(time) {
    		$("#formDate").datetimepicker({
		        format: 'yyyy-mm',
		        language: 'zh-CN',
		        todayHighlight: true,
		        maxView: 'decade',
		        minView: 3,
		        startView: 3,
		        todayBtn: true,
				autoclose: true
		    });
    		if (time != "") {
    			$("#formDate").datetimepicker('update', time);
    		}

    		// 获取当前月最后一天
    		let nowTime = ב.addDays($("#formDate").val(), 0),
    			lastDay = new Date(nowTime.getFullYear(), nowTime.getMonth()+1, 0).getDate() + 1;

    		let dayHtml = "";
    		for (let x = 1; x < lastDay; x++) {
    			if (new Date().getDate() - x == 1) {
    				dayHtml += "<li class=\"now\" type=\"day\""
    						+" date=\""+ ב.getTimex(new Date(nowTime.getFullYear(), nowTime.getMonth(), x)) +"\""
    						+" datex=\""+ ב.getTimex(new Date(nowTime.getFullYear(), nowTime.getMonth(), x), 'zh-cn') +"\""
    						+"><h>"+ x +"</h><span>日</span></li>";
    			} else {
    				dayHtml += "<li type=\"day\""
    						+" date=\""+ ב.getTimex(new Date(nowTime.getFullYear(), nowTime.getMonth(), x)) +"\""
    						+" datex=\""+ ב.getTimex(new Date(nowTime.getFullYear(), nowTime.getMonth(), x), 'zh-cn') +"\""
    						+"><h>"+ x +"</h><span>日</span></li>";
    			}
    		}
    		listBox.append(dayHtml);
    	}

    	// 旬
    	function timeTenDays(time) {
    		$("#formDate").datetimepicker({
		        format: 'yyyy-mm-dd',
		        language: 'zh-CN',
		        todayHighlight: true,
		        maxView: 'decade',
		        minView: 4,
		        startView: 4,
		        todayBtn: true,
		        autoclose: true
		    });
    		if (time != "") {
    			$("#formDate").datetimepicker('update', time);
    		}

    		// 获取当前月
    		let nowTime = new Date(),
				tenDaysHtml = "";
				
			for (let x = 1; x < 13; x++) {
				tenDaysHtml += "<li type=\"tenDays\""
    					+" date=\""+ ב.getTimex(new Date(nowTime.getFullYear(), (x-1), 1)) +"\""
    					+" datex=\""+ ב.getTimex(new Date(nowTime.getFullYear(), (x-1), 1), 'zh-cn') +"\""
    					+"><h>"+ x +"</h><span>上旬</span></li>";
				tenDaysHtml += "<li type=\"tenDays\""
							+" date=\""+ ב.getTimex(new Date(nowTime.getFullYear(), (x-1), 11)) +"\""
							+" datex=\""+ ב.getTimex(new Date(nowTime.getFullYear(), (x-1), 11), 'zh-cn') +"\""
							+"><h>"+ x +"</h><span>中旬</span></li>";
				tenDaysHtml += "<li type=\"tenDays\""
							+" date=\""+ ב.getTimex(new Date(nowTime.getFullYear(), (x-1), 21)) +"\""
							+" datex=\""+ ב.getTimex(new Date(nowTime.getFullYear(), (x-1), 21), 'zh-cn') +"\""
							+"><h>"+ x +"</h><span>下旬</span></li>";
			}
			listBox.append(tenDaysHtml);

    		// 判断时间范围
    		if (nowTime.getDate() <= 10) {
    			$(".timeList li").eq(nowTime.getMonth() * 3).addClass('now');
    		} else if (nowTime.getDate() > 10 && nowTime.getDate() <= 20) {
    			$(".timeList li").eq(nowTime.getMonth() * 3 + 1).addClass('now');
    		} else if (nowTime.getDate() > 20) {
    			$(".timeList li").eq(nowTime.getMonth() * 3 + 2).addClass('now');
    		}
    	}

    	// 月
    	function timeMonth(time) {
    		$("#formDate").datetimepicker({
		        format: 'yyyy-mm',
		        language: 'zh-CN',
		        todayHighlight: true,
		        maxView: 'decade',
		        minView: 4,
		        startView: 4,
		        todayBtn: true,
		        autoclose: true
		    });
    		if (time != "") {
    			$("#formDate").datetimepicker('update', time);
    		}

    		// 获取当前年
    		let nowTime = ב.addDays($("#formDate").val(), 0),
				monthHtml = "";
    		for (let x = 1; x < 13; x++) {
    			if (x-1 == nowTime.getMonth()) {
    				monthHtml += "<li class=\"now\" type=\"month\""
    							+" date=\""+ ב.getTimex(new Date(nowTime.getFullYear(), x - 1, 1)) +"\""
    							+" datex=\""+ ב.getTimex(new Date(nowTime.getFullYear(), x - 1, 1), 'zh-cn') +"\""
    							+"><h>"+ x +"</h><span>月</span></li>";
    			} else {
    				monthHtml += "<li type=\"month\""
    							+" date=\""+ ב.getTimex(new Date(nowTime.getFullYear(), x - 1, 1)) +"\""
    							+" datex=\""+ ב.getTimex(new Date(nowTime.getFullYear(), x - 1, 1), 'zh-cn') +"\""
    							+"><h>"+ x +"</h><span>月</span></li>";
    			}
    		}
    		listBox.append(monthHtml);
    	}

    	// 年
    	function timeYear(time) {
    		$("#formDate").datetimepicker({
		        format: 'yyyy',
		        language: 'zh-CN',
		        todayHighlight: true,
		        maxView: 'decade',
		        minView: 4,
		        startView: 4,
		        todayBtn: true,
		        autoclose: true
		    });
    		if (time != "") {
    			$("#formDate").datetimepicker('update', time);
    		}

    		let yearHtml = "",
    			nowYear = Number($("#formDate").val());
    		for (let x = nowYear; x < nowYear + 1; x++) {
    			yearHtml += "<li class=\"now\" type=\"year\""
    					+"date=\""+ ב.getTimex(new Date(nowYear, 0, 1)) +"\""
    					+"datex=\""+ ב.getTimex(new Date(nowYear, 0, 1), 'zh-cn') +"\""
    					+"><h>"+ x +"</h><span>年</span></li>";
    		}
    		listBox.append(yearHtml);
		}
		
		let bodyWid = $("body").width(),
			tsWid = $(".wrap_timeScale").width();
		$(".wrap_timeScale").css("left", (bodyWid - tsWid) / 2 + 'px');

    	$(".scale i").css('left', $(".timeList li:first").position().left + $(".timeList li:first").width() / 2 - 12);
    	$(".timeList li.now").click();
    }
})(onemap);